<%@page import="java.text.DateFormat"%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Date"%>
<%@ page import="com.google.appengine.api.datastore.DatastoreServiceFactory" %>
<%@ page import="com.google.appengine.api.datastore.DatastoreService" %>
<%@ page import="com.google.appengine.api.datastore.Cursor" %>
<%@ page import="com.google.appengine.api.datastore.Query" %>
<%@ page import="com.google.appengine.api.datastore.Entity" %>
<%@ page import="com.google.appengine.api.datastore.FetchOptions" %>
<%@ page import="com.google.appengine.api.datastore.Key" %>
<%@ page import="com.google.appengine.api.datastore.KeyFactory" %>
<%@ page import="com.google.appengine.api.datastore.QueryResultList" %>
<%@ page import="com.google.appengine.api.datastore.Query.Filter" %>
<%@ page import="com.google.appengine.api.datastore.Query.FilterOperator" %>
<%@ page import="com.google.appengine.api.datastore.Query.FilterPredicate" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>


<!DOCTYPE html>
<html>
<head>
  <link type="text/css" href="css/smoothness/jquery-ui-1.9.1.custom.css" rel="stylesheet" />
  <script src="js/jquery-1.8.2.js"></script>
  <script src="js/jquery-ui-1.9.1.custom.js"></script>
  <script src="palm.js"></script>
  <style>
  	.body{
  		margin: 0px;
  	}
  	.header{
  		background-color: #666666;
  		color: #ffffff;
  	}
  	.header #heading{
  		font-size:60px;
  		font-family:arial;
  		padding: 8px 12px;
  	}
  	.content{
  		margin-left: 10px;
  		margin-right:10px;
  		margin-top:10px;
  		margin-bottom: 10px;
  	}
  	.apost{
  		background-color: #666666;
  		color: #000000;
  		margin:5px, 5px;
  	}
  	.apost p,h1 {
  		color: #ffffff;
  	}
  	.footer{
  		background-color: #666666;
  		color: #ffffff;
  		position:relative;
  		height: 20px;
  		margin-top: -20px;
  		clear:both;
  	}
  </style>
</head>
<body leftmargin="0px" topmargin="0px" marginwidth="0px" marginheight="0px">
<div class="header">
	<div id="heading">
		Vote! Yes/No - Past Votes
	</div>
</div><!-- header -->

<div class="content">
<%
	int pageLimit = 10;
	DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    FetchOptions fopt = FetchOptions.Builder.withLimit(pageLimit);

	String theCur = request.getParameter("p");
	if(theCur!=null)
		fopt.startCursor(Cursor.fromWebSafeString(theCur));
	
    Query query = new Query("Post").setFilter(new FilterPredicate("valid",FilterOperator.EQUAL,"0"))
	//.addSort("valid",Query.SortDirection.ASCENDING)
	.addSort("date", Query.SortDirection.DESCENDING);
    QueryResultList<Entity> posts = datastore.prepare(query).asQueryResultList(fopt);
%>
	
<div id="allVote">
<% 
	if(posts.isEmpty()){ 
%> 
		<p>no vote</p>
<%
	}else{
		for(Entity post: posts ){
			Date createTime = (Date) post.getProperty("date");
%>
		<div class="apost" id="post-<%= post.getKey() %>">
			<h1><%= post.getProperty("topic")%></h1>
<%
			if(!post.getProperty("img1").equals("null")){
%>
			<img height="300" src="/votepost?blob-key=<%=post.getProperty("img1")%>"/>
<%
			}
			if(!post.getProperty("img2").equals("null")){
%>
			<img height="300" src="/votepost?blob-key=<%=post.getProperty("img2")%>"/>
<%
			}
%>
			<p>Yes: <%= post.getProperty("yes") %> No: <%= post.getProperty("no") %></p>
    		<p class="createtime">Created Time: <%=createTime %></p>
    		<hr>
			</div>
<%
		}
	}
%>
<br><br>
</div> <!--  all votes -->
<% 
	if(posts.size()>=pageLimit){
%>
				<a href="/?p=<%= posts.getCursor().toWebSafeString() %>">Next page</a>
<%
	}
%>

</div><!-- content -->
</body>
<div class="footer">
	<center>
		CopyRight (C) 2012 Alvin Hon, alvin.cpp@gmail.com
	</center>
</div>

</html>



